package com.miandian.controller;

import com.aliyuncs.exceptions.ClientException;
import com.miandian.common.R;
import com.miandian.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
//    @PostMapping
//    public Result upload(MultipartFile file) throws IOException {
//        String name =  file.getOriginalFilename();
//        int index = name.lastIndexOf(".");
//        String exname = name.substring(index);
//        System.out.println(name+ "+" +exname);
//        String newName = UUID.randomUUID().toString()+exname;
//        file.transferTo(new File("C:\\Users\\CYQ\\Desktop\\a\\file\\"+newName));
//        return Result.success();
//    }
    @PostMapping
    public R upload(MultipartFile file) throws IOException, ClientException {
        String url = aliOSSUtils.upload(file);
        log.info("文件上传"+url);

        return R.success(url);
    }
}
//String getOriginalFilename()  获取原始文件名
//void transferTo("位置")  将文件转移到磁盘
//long getSize()  获取文件大小  字节
//byte[] getBytes()  获取文件内容字节数组
//inputStream getInputStream()  //获取文件内容输出流